﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SharingCenter.Web.Classes.Profiles
{
    using SharingCenter.Web.Classes.enums;
    using SharingCenter.Web.Classes;
    using System.Xml.Linq;
    using SharingCenter.Web.Classes.Account;
    using System.Data.Linq;
    public static class ProfileOperations
    {
        public static Sc_Address GetAddress(Guid userId)
        {
            using (scDataContext cxt = new scDataContext())
            {
                return cxt.Sc_Addresses.SingleOrDefault(d => d.UserId == userId);
            }
        }

        public static string GetAvatarImagePath(Guid userId, AvatarType avatarType, Sc_Profile profile)
        {
            Sc_Profile p = profile ?? GetProfile(userId);
            return GetAvatarImagePathByProfile(ref p, avatarType);
        }

        static string GetAvatarImagePathByProfile(ref Sc_Profile p, AvatarType avatarType)
        {
            if (p != null)
            {
                string avatarmagePath = string.Empty;
                if (p.AvatarImage == null)
                {
                    avatarmagePath = GetDefaultAvatarImage(avatarType);
                }
                else
                {
                    avatarmagePath = WebConsts.AVATAR_FOLDER + "/" + p.AvatarImage.Element(avatarType.ToString()).Value;
                }

                return avatarmagePath;
            }
            else
            {
                return GetDefaultAvatarImage(avatarType);
            }
        }

        static string GetDefaultAvatarImage(AvatarType avatarType)
        {
            string avatarmagePath = string.Empty;
            switch (avatarType)
            {
                case AvatarType.Normal:
                    avatarmagePath = WebConsts.AVATAR_DEFAULT_IMG_PATH;
                    break;
                case AvatarType.Thumbnail:
                    avatarmagePath = WebConsts.AVATAR_DEFAULT_IMG_THUMBNAIL_PATH;
                    break;
                default:
                    avatarmagePath = WebConsts.AVATAR_DEFAULT_IMG_TINY_PATH;
                    break;
            }
             return avatarmagePath;
        }

        public static Sc_Profile GetProfile(Guid userId)
        {
            using (scDataContext cxt = new scDataContext())
            {
                return cxt.Sc_Profiles.SingleOrDefault(d => d.UserId == userId);
            }
        }

        public static Sc_Profile GetProfile(Guid userId, out scDataContext dc)
        {
            dc = new scDataContext();
            return dc.Sc_Profiles.SingleOrDefault(d => d.UserId == userId);
        }

        public static void AddNewAddress(Guid userId)
        {
            using (scDataContext cxt = new scDataContext())
            {
                Sc_Address a = new Sc_Address();
                a.UserId = userId;
                a.Address = "";
                a.Mobile = "";
                a.Phone = "";
                a.PostalCode = "";
                a.Messengers = null;
                cxt.Sc_Addresses.InsertOnSubmit(a);
                cxt.SubmitChanges();
            }
        }

        static void InitializePrivacy(Guid userId)
        {
            if (scDataContext.Instance.Sc_Privacies.SingleOrDefault(d => d.UserId == userId) == null)
            {
                Sc_Privacy p = new Sc_Privacy();
                p.UserId = userId;
                p.IsSearchable = true;
                p.FriendRequestType = FriendRequestTypes.Anyone;
                scDataContext.Instance.Sc_Privacies.InsertOnSubmit(p);
            }
        }

        static void InitializeAddress(Guid userId)
        {
            if (scDataContext.Instance.Sc_Addresses.SingleOrDefault(d => d.UserId == userId) == null)
            {
                Sc_Address a = new Sc_Address();
                a.UserId = userId;
                scDataContext.Instance.Sc_Addresses.InsertOnSubmit(a);
            }
        }


        public static bool InitializeProfile(string userName, string nickName, string nickNamePinyin)
        {
            bool ret = false;
            aspnet_User u = AccountOperations.GetUser(userName);
            if (u != null)
            {
                Sc_Profile p = GetProfile(u.UserId);
                if (p == null)
                {
                    p = new Sc_Profile();
                    p.NickName = nickName;
                    p.UserId = u.UserId;
                    p.Pinyin = nickNamePinyin;
                    p.BirthDate = DateTime.Today.AddYears(-18);
                    p.Gender = SharingCenter.Web.Classes.enums.Gender.NA;
                    scDataContext.Instance.Sc_Profiles.InsertOnSubmit(p);

                    InitializeAddress(u.UserId);
                    InitializePrivacy(u.UserId);

                    try
                    {
                        WebProfile pro = new WebProfile(WebProfile.Create(userName));
                        pro.UserId = u.UserId;
                        pro.Personal.NickName = nickName;
                        pro.Website.ProfileUpdated = false;
                        pro.Save();
                    }
                    catch { }

                    try
                    {
                        scDataContext.Instance.SubmitChanges(ConflictMode.FailOnFirstConflict);
                        ret = true;
                    }
                    catch { }
                }
            }

            return ret;
        }
    }
}
